﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.DataAccess;
using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Workbench.Services;

namespace SharedGenomics.Workbench.Presenters
{
    public class FileSetUploadPresenter : Presenter
    {
        private bool _genotypeFileLoaded;
        private bool _phenotypeFileLoaded;

        public FileSetUploadPresenter(IFileSetUploadView view, IOpenGenotypeFilePresenter genotypeFilePresenter, IOpenPhenotypeFilePresenter phenotypeFilePresenter, IFileSetUploadService fileSetUpload, IFileUpload fileUpload)
            : base(view)
        {
            this.UploadView = view;
            this.OpenGenotypeFilePresenter = genotypeFilePresenter;
            this.OpenPhenotypeFilePresenter = phenotypeFilePresenter;
            this.FileSetService = fileSetUpload;
            this.FileUpload = fileUpload;

            this.OpenGenotypeFilePresenter.GenotypeFileLoaded += new EventHandler(this.OpenGenotypePresenter_GenotypeFileLoaded);
            this.OpenPhenotypeFilePresenter.PhenotypeFileLoaded += new EventHandler(this.OpenPhenotypePresenter_PhenotypeFileLoaded);

            this.UploadView.UploadButtonClicked += new EventHandler(this.View_UploadButtonClicked);

            //upload button should be disabled initially
            this.UploadView.IsUploadEnabled = false;
        }

        private void OpenPhenotypePresenter_PhenotypeFileLoaded(object sender, EventArgs e)
        {
            this._phenotypeFileLoaded = true;
            this.CheckUploadEnabled();
        }

        private void OpenGenotypePresenter_GenotypeFileLoaded(object sender, EventArgs e)
        {
            this._genotypeFileLoaded = true;
            this.CheckUploadEnabled();
        }

        private void View_UploadButtonClicked(object sender, EventArgs e)
        {
            //get the local genotype and phenotype file names
            string localPhenotypeFilePath = this.OpenPhenotypeFilePresenter.PhenotypeFilePath;
            string localGenotypeFilePath = this.OpenGenotypeFilePresenter.GenotypeFilePath;

            string fileSetName = this.UploadView.DataSetName;
            this.UploadView.ServiceMessage = String.Empty;

            try
            {
                int fileSchemaId = this.OpenPhenotypeFilePresenter.SelectedFileSchemaId;

                //try to create the file set - if this succeeds then upload the two data files
                this.FileSetService.CreateFileSet(fileSetName, fileSchemaId, this.OpenGenotypeFilePresenter.GenotypeFileFormat).BindAction(uploadInfo =>
                {
                    this.FileUpload.UploadFile(localGenotypeFilePath, uploadInfo.RemoteGenotypeFileUri);
                    this.FileUpload.UploadFile(localPhenotypeFilePath, uploadInfo.RemotePhenotypeFileUri);

                    this.UploadView.ServiceMessage = String.Format("File upload for file set '{0}' in progress", fileSetName);
                });
            }
            catch (Exception ex)
            {
                this.UploadView.ServiceMessage = String.Format("Error creating file set: {0}", ex.Message);
            }
        }

        private void CheckUploadEnabled()
        {
            if (this._genotypeFileLoaded && this._phenotypeFileLoaded)
            {
                this.UploadView.IsUploadEnabled = true;
            }
        }

        [Region("OpenGenotypeFile")]
        public IOpenGenotypeFilePresenter OpenGenotypeFilePresenter { get; private set; }

        [Region("OpenPhenotypeFile")]
        public IOpenPhenotypeFilePresenter OpenPhenotypeFilePresenter { get; private set; }

        private IFileSetUploadView UploadView { get; set; }
        private IFileSetUploadService FileSetService { get; set; }
        private IFileUpload FileUpload { get; set; }
    }
}
